package com.axonactive.yolo.service.internal;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.axonactive.yolo.exception.SubcribedFlightNotFoundException;
import com.axonactive.yolo.monitor.ServiceProvider;
import com.axonactive.yolo.monitor.FlightObserverable;
import com.axonactive.yolo.monitor.FlightSubscriber;
import com.axonactive.yolo.monitor.Observerable;
import com.axonactive.yolo.service.interfaces.EmailService;
import com.axonactive.yolo.service.interfaces.ExchangeRateService;
import com.axonactive.yolo.service.interfaces.FlightNotificationService;
import com.axonactive.yolo.service.interfaces.ParamLockerService;
import com.axonactive.yolo.service.interfaces.SubscribedFlightService;
import com.axonactive.yolo.service.interfaces.SubscribedFlightUserIdService;
import com.axonactive.yolo.service.interfaces.UserEmailService;
import com.axonactive.yolo.service.interfaces.UserService;

@PropertySource("classpath:messages.properties")
public class Detector {
	@Resource
	private Environment env;

	@Autowired
	EmailService emailService;

	@Autowired
	private SubscribedFlightService subcribedFlightService;

	@Autowired
	private SubscribedFlightUserIdService subscribedFlightUserIdService;

	@Autowired
	private UserEmailService userEmailService;

	@Autowired
	private UserService userService;

	@Autowired
	private FlightNotificationService flightNotificationService;

	@Autowired
	private ParamLockerService paramLockerService;

	@Autowired
	private ExchangeRateService exchangeRateService;

	private final static Logger LOGGER = Logger.getLogger(Detector.class);

	public String printCurrentTime() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}
	public static boolean ISFINISHED = true;

	public void detect() throws SubcribedFlightNotFoundException {
			ISFINISHED=false;
			injectService();
			Observerable observerable = new FlightObserverable();
			FlightSubscriber subscriber = new FlightSubscriber(observerable);
			observerable.detect();
			ISFINISHED=true;
	}

	private void injectService()
	{
		ServiceProvider serviceProvider=ServiceProvider.getInstance();
		serviceProvider.injectService(subcribedFlightService);
		serviceProvider.injectService(emailService);
		serviceProvider.injectService(userEmailService);
		serviceProvider.injectService(subscribedFlightUserIdService);
		serviceProvider.injectService(flightNotificationService);
		serviceProvider.injectService(paramLockerService);
		serviceProvider.injectService(userService);
		serviceProvider.injectService(exchangeRateService);
	}
}
